package cn.edu.njpi.controller;

import cn.edu.njpi.model.TUser;
import cn.edu.njpi.utils.FileUtils;
import cn.edu.njpi.utils.InformUtils;
import cn.edu.njpi.utils.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author 朱宗辉
 * @version V1.0
 * @Package cn.edu.njpi.controller
 * @date 2022/4/20 12:38 PM
 */
@RestController
@RequestMapping(value = "/api/welcome")
public class WelcomeController {
    @Value("${welcome.file.url}")
    private String welcomeFileUrl;

    @RequestMapping(value = "/change")
    public T changeText(String text, @RequestHeader("token") String token) throws IOException {
        T t = new T();
        TUser tUser = (TUser) InformUtils.map.get(token);
        if (tUser != null) {
            if (tUser.getUserPermssionId() == 1) {
                String s = FileUtils.writeFile(welcomeFileUrl, text);
                t.add("welcome", s);
                t.setStatus(200);
            } else {
                t.add("error", "您的权限不足");
                t.setStatus(403);
            }
        } else {
            t.add("error", "未登录，请登录后再次请求！");
            t.setStatus(403);
        }
        return t;
    }
}
